﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Text;

namespace XCI.Core.Helper
{
    public static class ImageMetadataHelper
    {
        public static string LoadTags(Image image)
        {
            return ImageMetadataHelper.LoadTags(image, 800);
        }

        public static string LoadTags(Image image, int id)
        {
            string empty = string.Empty;
            try
            {
                empty = Encoding.ASCII.GetString(image.GetPropertyItem(id).Value);
            }
            catch
            {
            }
            return empty;
        }

        public static PropertyItem CreatePropertyItem(int id, string data)
        {
            PropertyItem propertyItem = ImageMetadataHelper.CreatePropertyItem();
            propertyItem.Id = id;
            propertyItem.Value = Encoding.ASCII.GetBytes(data + "\0");
            propertyItem.Len = propertyItem.Value.Length;
            propertyItem.Type = (short)2;
            return propertyItem;
        }

        private static PropertyItem CreatePropertyItem()
        {
            return (PropertyItem)typeof(PropertyItem).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, (Binder)null, new Type[0], (ParameterModifier[])null).Invoke((object[])null);
        }
    }
}
